import time
from public.action import *
from public.business import *
from public.testData import *
from public.tools import*

testCase="DBshop002_余额支付流程"
# 登录后台 给账号充值
# firefox=openBrowser(urlAdmin)
# DBshopLoginAdmin(firefox)
# DBshopEnterMenu2(firefox, "客户管理", "管理客户")
# # 给用户充值
# DBshopMoneyChange(firefox,userName,money)
# time.sleep(1)
# # 退出后台
# firefox.quit()
# 登录前台购买商品
firefox=openBrowser(url)
DBshopLogin(firefox,userName,password)
# 搜索小米
firefox.find_element_by_xpath("//input[@name='keywords']").send_keys("小米")
# 点击搜索
firefox.find_element_by_xpath("//input[@class='search-btn iconfont']").click()
time.sleep(2)
firefox.find_element_by_link_text("小米Mix3 全网通版 8GB+128GB 黑色 磁动力滑盖全面屏").click()
firefox.switch_to.window(firefox.window_handles[-1])
firefox.find_element_by_id("add_cart_submit").click()
firefox.find_element_by_link_text("去购物车结算").click()
firefox.find_element_by_link_text("去结算").click()
firefox.find_element_by_xpath("//input[@value='下一步']").click()
firefox.find_element_by_xpath("//input[@value='yezf']").click()
firefox.find_element_by_xpath("//input[@value='确认订单']").click()
# 提取订单编号
orderID=firefox.find_element_by_xpath("(//font[@color='color:#ED145B'])[2]").text
rootPath=get_pro_rootpath()
writeFile(rootPath+r'\public\order1.txt',orderID)
# print(orderID)
time.sleep(1)
firefox.find_element_by_xpath("//input[@value='马上去支付']").click()
firefox.quit()

# 确认收货
firefox=openBrowser(urlAdmin)
DBshopLoginAdmin(firefox)
DBshopEnterMenu2(firefox,"销售管理","订单管理")
firefox.find_element_by_xpath("//input[@name='order_sn']").send_keys(orderID)
firefox.find_element_by_xpath("//button[@class='btn btn-small btn-primary']").click()
firefox.find_element_by_link_text("编辑查看").click()
firefox.find_element_by_link_text("发货操作").click()
firefox.find_element_by_id("express_number").send_keys(kuaidi)
firefox.find_element_by_id("state_info").send_keys(note)
# 点击发货
firefox.find_element_by_xpath("//button").click()
firefox.quit()
# 前台确认收货
firefox=openBrowser(url)
DBshopLogin(firefox,userName,password)
firefox.find_element_by_link_text("我的订单").click()
firefox.find_element_by_partial_link_text("已发货").click()
firefox.find_element_by_xpath("//button[text()='确认收货']").click()
firefox.find_element_by_xpath("//button").click()
firefox.switch_to.alert.accept()
# 查看订单状态
firefox.find_element_by_partial_link_text("交易完成").click()
firefox.find_element_by_name("search_order_sn").send_keys(orderID)
firefox.find_element_by_xpath("//button[text()='搜索']").click()
# 断言
try:
    firefox.find_element_by_link_text(orderID).is_displayed() # is_displayed()函数判断元素是否显示
    writeLog("%s 测试通过"%testCase)
except:
    writeLog("%s 测试失败"%testCase)
    getPhoto(firefox)

firefox.quit()


